import {Injectable} from '@angular/core';
import {HttpClient} from '@angular/common/http';

@Injectable({
  providedIn: 'root'
})
export class AssessPriceService {
  serviceUrl = {
    addAndModifyTemplate: '/recycle/evalMould/addOrUpdateMould', // 新增或修改模版
    templateList: '/recycle/evalMould/list', // 模版列表
    deleteTemplate: '/recycle/evalMould/deleteMould', // 删除模版
    refreshTemplate: '/recycle/evalMould/updateModel', // 更新估价模版
    typeAssessList: '/recycle/model/modelEvalList', // 机型估价列表
    addAndModifyTypeAssess: '/recycle/model/updateModelEval', // 新增或修改机型估价
    addQuestion: '/evalQuestion/save', // 添加问题
    updateQuestion: '/evalQuestion/update', // 修改问题
    getQuestionList: '/evalQuestion/list', // 获取问题列表
    getQuestionDetail: '/evalQuestion/details', // 获取问题详情
    deleteTypeAssess: '/recycle/model/deleteModelEval', // 删除机型估价
    assessPrice: '/recycle/order/eval', // 估价
    getOrderExcel: '/recycle/model/getOrderExcel', // 导出机型估价列表
    templateOptions: '/recycle/model/findModelEvalList', // 估价目标列表下拉框
    templateDetail: '/recycle/model/findModelEvalDetails', // 估价模版详情
    fileUpload: '/common/upload/file', // 文件上传
    exportModelBenchmarkPriceUrl: '/recycle/model/getModelExcel', // 机型评估列表导出基准价
  };

  constructor(private httpClient: HttpClient) {
  }

  /**
   * 获取估价模版列表
   * @param title 标题
   * @param remarks 备注
   * @param startTime 开始时间
   * @param endTime 结束时间
   * @param page 页码
   * @param pageSize 分页大小
   */
  getTemplateList(title: string, remarks: string, startTime: string, endTime: string, page = 0, pageSize = 10) {
    return this.httpPost(this.serviceUrl.templateList, {title, remarks, startTime, endTime, page, pageSize});
  }

  /**
   * 添加问题
   * @param recycleEvalQuestion 问题基础信息
   * @param recycleEvalOptionList 问题选项
   */
  addQuestion(recycleEvalQuestion: any, recycleEvalOptionList: any) {
    return this.httpPost(this.serviceUrl.addQuestion, {recycleEvalQuestion, recycleEvalOptionList});
  }

  /**
   * 修改问题
   * @param recycleEvalQuestion 问题基础信息
   * @param recycleEvalOptionList 问题选项
   */
  updateQuestion(recycleEvalQuestion: any, recycleEvalOptionList: any) {
    return this.httpPost(this.serviceUrl.updateQuestion, {recycleEvalQuestion, recycleEvalOptionList});
  }

  /**
   * 获取问题列表
   * @param questionName 问题名称
   * @param beginTime 开始时间
   * @param endTime 结束时间
   * @param sort 排序 0正序 1倒序
   * @param page 页码
   * @param pageSize 分页大小
   */
  getQuestionList(page: number, pageSize: number, questionName: string, beginTime: string, endTime: string, sort = 1) {
    return this.httpPost(this.serviceUrl.getQuestionList, {page, pageSize, questionName, beginTime, endTime, sort});
  }

  /**
   * 获取问题详情
   * @param id 问题id
   */
  getQuestionDetail(id: string | number) {
    return this.httpPost(this.serviceUrl.getQuestionDetail, {id});
  }

  /**
   * 新增或修改模版
   * @param title 标题
   * @param remarks 备注
   * @param context 模板内容
   * @param id 模版Id
   * @param problemId 问题Id
   * @param optionId 选项Id
   */
  addAndModifyTemplate(title: string, remarks: string, context: string, problemId: string, optionId: string, id?: any) {
    return this.httpPost(this.serviceUrl.addAndModifyTemplate, {title, remarks, context, problemId, optionId, id});
  }

  /**
   * 删除模版
   * @param id 模版Id
   */
  deleteTemplate(id: any) {
    return this.httpGet(this.serviceUrl.deleteTemplate, {id});
  }

  /**
   * 获取机型估价列表
   * @param page 当前页
   * @param pageSize 分页大小
   * @param name 模版名称
   * @param brandId 品牌id
   * @param seriesId 系列id
   * @param evalStatus 状态
   * @param evalId 估价id
   * @param remarks 内部备注
   * @param startTime 开始时间
   * @param endTime 结束时间
   */
  getTypeAssessList(page: number, pageSize: number, name: string, brandId: string, seriesId: string, evalId: string, evalStatus: any, remarks: any, startTime: string, endTime: string) {
    return this.httpPost(this.serviceUrl.typeAssessList, {
      page,
      pageSize,
      name,
      brandId,
      seriesId,
      evalId,
      evalStatus,
      remarks,
      startTime,
      endTime
    });
  }


  /**
   * 获取机型估价列表
   * @param newPrice 准新基准价
   * @param secondHandPrice 二手基准价
   * @param evalId 模板id
   * @param evaluationId 模板id
   * @param remarks 备注
   * @param evalStatus 状态
   * @param  contextImproved 新内容 context 内容
   * @param id Id
   * @param isAdd 是否是新增
   */
  addAndModifyTypeAssess(
    newPrice: number,
    secondHandPrice: number,
    evalId: string,
    evaluationId: string,
    remarks: string,
    evalStatus: number,
    contextImproved: string,
    id: string,
    isAdd: boolean) {
    return this.httpPost(this.serviceUrl.addAndModifyTypeAssess, {
      newPrice,
      secondHandPrice,
      evalId,
      remarks,
      evaluationId,
      evalStatus,
      contextImproved,
      id,
      isAdd
    });
  }

  /**
   * 删除机型估价
   * @param body 模版Id evaluationId  启用状态evalStatus
   */
  deleteTypeAssess(body: any) {
    return this.httpGet(this.serviceUrl.deleteTypeAssess, body);
  }

  /**
   * 删除机型估价
   * @param mid 机型Id
   * @param opts 机型Id
   */
  assessPrice(mid: any, opts: string) {
    return this.httpGet(this.serviceUrl.assessPrice, {mid, opts, platform: '2'});
  }

  /**
   * 更新估价模版
   */
  refreshTemplate() {
    return this.httpGet(this.serviceUrl.refreshTemplate, {});
  }

  /**
   * 获取估价模版详情
   */
  getTemplateDetail(id: any) {
    return this.httpGet(this.serviceUrl.templateDetail, {id});
  }

  /**
   * 获取估价模版下拉数据
   */
  getTemplateOptions() {
    return this.httpGet(this.serviceUrl.templateOptions, {});
  }

  formatTime(date: any, type: 'date' | 'dateTime' | 'time') {
    if (!date || (typeof date) === 'string') {
      return date || '';
    }
    let result = '';
    switch (type) {
      case 'date':
        // @ts-ignore
        result = date.toISOString().match(/\d{4}-\d{2}-\d{2}/)[0];
        break;
      case 'dateTime':
        // @ts-ignore
        result = date.toISOString().match(/\d{4}-\d{2}-\d{2}/)[0] + ' ' + date.toString().match(/\d{2}:\d{2}:\d{2}/)[0];
        break;
      case 'time':
        // @ts-ignore
        result = date.toString().match(/\d{2}:\d{2}:\d{2}/)[0];
        break;
    }
    return result;
  }

  getOrderExcel(params: any) {
    return this.httpClient.get(this.serviceUrl.getOrderExcel, {
      params,
      responseType: 'blob',
      observe: 'response',
    }).toPromise();
  }

  httpGet(url: string, params: any) {
    return this.httpClient.get(url, {params}).toPromise();
  }

  httpPost(url: string, body: any) {
    return this.httpClient.post(url, body).toPromise();
  }

  // 机型列表导出基准价
  exportModelBenchmarkPriceData(params: any) {
    return this.httpClient.get(this.serviceUrl.exportModelBenchmarkPriceUrl, { params: params, responseType: 'blob' }).toPromise();
  }
}
